﻿using EMoney.IStock.Client.Business;
using EMoney.IStock.Client.Business.CQ;
using EMoney.IStock.Client.Models.CQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace EMoney.IStock.Client.Web.Controllers
{
    public class CQController : Controller
    {
        /// <summary>
        /// 我是传奇
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            int userid = 0;
            if (!string.IsNullOrEmpty(Request["token"]))    //token有值时，执行认证操作
            {
                userid = TokenBll.GetUserIDByToken(Request["token"]);
            }
            else
            {
                userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            }

            MyGuessBll myGuessBll = new MyGuessBll();

            
            ViewBag.CQGuess = myGuessBll.GetCQ();   //我是传奇 得主
            
            //竞猜日期
            bool canGuess = false;
            DateTime guessDate = myGuessBll.GetGuessDate(out canGuess);

            ViewBag.GuessDate = guessDate;
            ViewBag.CanGuess = canGuess;

            if (userid != 0)
            {
                ViewBag.MyGuess = myGuessBll.GetMyGuess(userid);    //已登录用户显示 我的信息（头像、昵称、竞猜战绩、最近竞猜）
            }

            //目前幸存人数

            //上一交易日竞猜结果胜负比例
            ViewBag.PreGuessResult = myGuessBll.GetGuessResult();

            //晋级人数
            ViewBag.PromotionOutCount = myGuessBll.GetPromotionOutCount();

            //当前总竞猜人数
            ViewBag.GuessTotal = myGuessBll.GetGuessUserCount();

            return View();
        }

        /// <summary>
        /// 竞猜
        /// </summary>
        /// <returns></returns>
        public string guess()
        {
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid != 0)
            {
                string reqFlag = Request["flag"];
                string reqGuessDate = Request["date"];

                if (string.IsNullOrEmpty(reqFlag) || string.IsNullOrEmpty(reqGuessDate))
                {
                    return string.Empty;
                }

                int flag = 0;

                if (int.TryParse(reqFlag, out flag))
                {
                    if (flag != 0 && flag != 1)
                    {
                        return string.Empty;
                    }
                }
                else
                {
                    return string.Empty;
                }

                DateTime guessDate = DateTime.MinValue;

                if (!DateTime.TryParse(reqGuessDate, out guessDate))    //竞猜日期格式错误
                {
                    return string.Empty;
                }

                MyGuessBll myGuessBll = new MyGuessBll();

                return myGuessBll.Guess(userid, guessDate, flag);
            }
            else
            {
                return string.Empty;
            }
        }

        //private string GetClientIP()
        //{
        //    string ip = Request.UserHostAddress;

        //    if (ip.Equals("::1", StringComparison.OrdinalIgnoreCase))
        //    {
        //        ip = "192.168.6.134";
        //    }

        //    return ip;
        //}

        //private int GetGuessUserID()
        //{
        //    int userid = 0;
        //    string ip = Request.UserHostAddress;

        //    if (ip.Equals("::1", StringComparison.OrdinalIgnoreCase))
        //    {
        //        ip = "192.168.6.134";
        //    }

        //    string[] arrIP = ip.Split('.');

        //    if (arrIP != null || arrIP.Length == 4)
        //    {
        //        userid = Convert.ToInt32(arrIP[2] + arrIP[3]);
        //    }

        //    return userid;
        //}
    }
}
